#include "BasicHistogramGUI.h"
#include "../gui/MainWindow/MainWindow.h"
namespace LightChart {
BasicHistogramGUI::BasicHistogramGUI(QWidget *parent) : QDialog(parent) {
    setupUi(this);
    const GlobalChartSettings& global = GlobalChartSettings::get_instance();
    lineEditTitle->setText(global.chartTitle.c_str());
    lineEditWidth->setText(QString::number(global.width));
    lineEditHeight->setText(QString::number(global.height));
    graphicsView->setStyleSheet(getBgColorStyleSheet(global.bgColor));
    connect(pushButtonBgColor,SIGNAL(clicked()),this,SLOT(onButtonBgColorClicked()));
    connect(pushButtonLineColors,SIGNAL(clicked()),this,SLOT(onButtonLineColorsClicked()));
    connect(cancelButton,SIGNAL(clicked()),this,SLOT(onButtonCancelClicked()));
    connect(saveButton,SIGNAL(clicked()),this,SLOT(onButtonSaveClicked()));
}
void BasicHistogramGUI::onButtonBgColorClicked() {
    color = QColorDialog::getColor(Qt::white);
    graphicsView->setStyleSheet(getBgColorStyleSheet(color));
}
void BasicHistogramGUI::onButtonCancelClicked() {
    hide();
}
void BasicHistogramGUI::onButtonLineColorsClicked() {

}
void BasicHistogramGUI::onButtonSaveClicked() {
    try {
        if(!lineEditWidth->text().isEmpty()) {
            double tmp = QStringToDouble(lineEditWidth->text());
            if(tmp < 100 || tmp > 1920) throw std::runtime_error("Wrong width specified. Width must be in [100 : 1920]");
        }
        if(!lineEditHeight->text().isEmpty()) {
            double tmp = QStringToDouble(lineEditHeight->text());
            if(tmp < 50 || tmp > 1080) throw std::runtime_error("Wrong height specified. Height must be in [50 : 1080]");
        }
        if(!lineEditMin->text().isEmpty()) {
            QStringToDouble(lineEditMin->text());
        }
        if(!lineEditMax->text().isEmpty()) {
            QStringToDouble(lineEditMax->text());
        }
        hide();
    }
    catch(const std::runtime_error& e) {
        QMessageBox::critical(this,"Input data corrupted.",e.what());
    }
    static_cast<MainWindow*>(parentWidget())->RefreshAll();
}
}
